#include<bits/stdc++.h>
#include "script_manager.h"
using namespace std;

script_manager::script_manager(){
    string sm_password = "";
};
script_manager::script_manager(string sm_password):sm_password(sm_password) {}
void script_manager::inputbook(script_item* input, int n) {
    int val = script_id.get(get_hash(input->name));
    puts("录入图书为: ");
    cout << "名称: " << input->name << endl << "出版社: " << input->publisher << endl
        << "作者: " << input->Writer << endl << "版号: " << input->ISBN << endl;
    if (val == -1) {
        script_id.add(get_hash(input->name), ++total_book_items);
        input->id=total_book_items;
        node_sc.insert(input);
        script_item* item = node_sc.Find(total_book_items);
        item->res_num += n;
        return;
    }
    script_item* item = node_sc.Find(val);
    item->res_num += n;
    return;
}
void script_manager::removebook(std::string name, int n) {
    int val = script_id.get(get_hash(name));
    if (val == -1) {
        puts("该条目不存在! ");
        return;
    }
    script_item* item = node_sc.Find(val);
    item->res_num-=n;
    if(item->res_num<0){
        item->res_num=0;
    }
    puts("删除成功!");
    cout<<"剩余库存数量: "<<item->res_num<<endl;
    return;
}
void traverse_by_publisher(std::string publisher, vector<script_item>& rec, Node* cursor = node_sc.getRoot()) {
    if (cursor == NULL) {
        return;
    }
    for (int i = 0; i < cursor->size; i++) {
        if (cursor->key[i].publisher == publisher) {
            rec.push_back(cursor->key[i]);
        }
    }
    if (!cursor->IS_LEAF) {
        for (int i = 0; i < cursor->size + 1; i++) {
            traverse_by_publisher(publisher,rec,cursor->ptr[i]);
        }
    }
}
void script_manager::display_by_publisher(std::string publisher) {
    vector<script_item>vec;
    traverse_by_publisher(publisher, vec,node_sc.getRoot());
    puts("该出版社下的图书为: ");
    int cnt=0;
    for(auto c:vec){
        cout<<++cnt<<"."<<endl;
        cout<<"名称: "<<c.name<<endl<<"作者: "<<c.Writer<<"库存数量: "<<c.res_num<<endl<<"版号: "<<c.ISBN<<endl;
    }
}
void traverse_by_writer(std::string Writer, vector<script_item>& rec, Node* cursor) {
    if (cursor == NULL) {
        return;
    }
    for (int i = 0; i < cursor->size; i++) {
        if (cursor->key[i].Writer == Writer) {
            rec.push_back(cursor->key[i]);
        }
    }
    if (!cursor->IS_LEAF) {
        for (int i = 0; i < cursor->size + 1; i++) {
            traverse_by_publisher(Writer,rec,cursor->ptr[i]);
        }
    }
}
void script_manager::display_by_writer(std::string Writer) {
    vector<script_item>vec;
    traverse_by_publisher(Writer,vec,node_sc.getRoot());  
    puts("该作者名下的图书有: ");
    int cnt=0;
    for(auto c:vec){
        cout<<++cnt<<"."<<endl;
        cout<<"名称: "<<c.name<<endl<<"出版社: "<<c.publisher<<endl<<"版号: "<<c.ISBN<<endl<<"库存数量: "<<c.res_num<<endl;
    }
}
void script_manager::display_bro(std::string name) {
    int val = script_id.get(get_hash(name));
    if (val == -1) {
        puts("该条目不存在!");
        return;
    }
    script_item *input=node_sc.Find(val);
    vector<brorrow_item>v=input->items_b;
    puts("该图书的借阅信息为: ");
    for(auto c:v){
       cout<<"用户: "<<c.borer<<endl<<"图书: "<<c.script_name<<endl<<"具体的还书时间: "<<ctime(&c.bro_time)<<endl;
    }
}
//插入 删除 
//展示某一作者/出版社的所有图书信息 查询某一图书条目的全部借阅记录